package com.arthur.controller;

import com.arthur.util.DocConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

/**
 * Created by Administrator on 2017/6/15.
 */
@Controller
public class ViewController {

    @RequestMapping(value = "/upload",method = RequestMethod.GET)
    public String showUploadPage(){
        return "fileUpload";
    }

    @RequestMapping(value = "/viewer",method = RequestMethod.GET)
    public String showViewer(){
        return "documentView";
    }

    @RequestMapping(value = "/convert",method = RequestMethod.POST)
    public String convert(HttpServletRequest request, HttpServletResponse response
            , @RequestParam(value = "file") CommonsMultipartFile file) throws IOException{
        /** 构建文件保存的目录* */
        String saveDirectory =request.getSession().getServletContext().getRealPath("/")+"upload"+"/";
        String filename = ""+System.currentTimeMillis();
        String extname = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String path = saveDirectory+filename+extname;

        System.out.println("00000"+path);
        File newFile=new File(path);
        file.transferTo(newFile);

        DocConverter d = new DocConverter(path);
        d.conver();
        System.out.println("------"+d.getswfPath());
        String swfpath = "upload"+d.getswfPath().substring(d.getswfPath().lastIndexOf("/"));
        request.getSession().setAttribute("swfpath", swfpath);

        return "redirect:/viewer";
    }
}
